package com.xtpt.disaster.controller;

import com.xtpt.disaster.bean.BackupFileInfo;
import com.xtpt.disaster.common.PortalResult;
import com.xtpt.disaster.service.BackupFileInfoManageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@CrossOrigin
@RequestMapping(value = "/manage/bpfinfo")
public class BackupFileInfoManageController {
    @Autowired
    BackupFileInfoManageService backupFileInfoManageService;

    /*
     * @Author jack xin
     * @Method Name insertAll
     * @Description TODO
     * @Param [backupFileInfo]
     * @return com.xtpt.disaster.common.PortalResult
     * @Date 14:42 2021/11/16
    **/
    @PostMapping(value = "/insert")
    public PortalResult insertAll(@RequestBody BackupFileInfo backupFileInfo) {
        int res = backupFileInfoManageService.insertBackupInfo(backupFileInfo);
        if (res == 0) {
            return PortalResult.error("fasle");
        }
        else {
            return PortalResult.ok(res);
        }
    }

    @DeleteMapping(value = "/delete")
    public PortalResult deleteById(@RequestParam(value = "id") Long id) {
        int res = backupFileInfoManageService.deleteById(id);
        if (res == 0) {
            return PortalResult.error("res");
        }
        else {
            return PortalResult.ok(res);
        }
    }

    @PostMapping(value = "/batch/delete")
    public PortalResult deleteBatch(@RequestBody long[] ids) {
        int res = backupFileInfoManageService.deleteBatch(ids);
        if (res == 0) {
            return PortalResult.error("res");
        }
        else {
            return PortalResult.ok(res);
        }
    }

    @GetMapping(value = "/getbyid")
    public PortalResult getAllById(@RequestParam(value = "id") Long id) {
        BackupFileInfo data = backupFileInfoManageService.selectBackupFileById(id);
        return PortalResult.ok(data);
    }

    @PutMapping(value = "/update")
    public PortalResult updateById(@RequestBody BackupFileInfo backupFileInfo) {
        int res = backupFileInfoManageService.updateBackupFileByIdSelective(backupFileInfo);
        if (res == 0) {
            return PortalResult.error("fasle");
        }
        else {
            return PortalResult.ok(res);
        }
    }

    @GetMapping(value = "/getall")
    public PortalResult getAllById() {
        List<BackupFileInfo> data = backupFileInfoManageService.getAllBackupFileInfo();
        return PortalResult.ok(data);
    }
}
